package com.yc.cloud.api.service.impl;

import cn.hutool.core.map.MapUtil;
import com.yc.cloud.api.service.MemberAccountCacheService;
import com.yc.cloud.basic.constants.RedisKeyConstants;
import com.yc.cloud.basic.service.RedisService;
import com.yc.cloud.basic.utils.MyFastJsonUtils;
import com.yc.cloud.entity.MemberAccount;
import lombok.val;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MemberAccountCacheServiceImpl implements MemberAccountCacheService {

    @Resource
    private RedisService<MemberAccount> redisService;


    @Override
    public MemberAccount getByKey(String key) {
        val memberAccountMap = redisService.hGetAll(key);
        if (MapUtil.isNotEmpty(memberAccountMap)) {
            val memberAccount = (MemberAccount) MyFastJsonUtils.map2Object(memberAccountMap, MemberAccount.class);
            if (memberAccount != null && memberAccount.getId() != null) {
                return memberAccount;
            }
        }
        return null;
    }

    @Override
    public void setByKey(String key, MemberAccount memberAccount) {
        //对象转map
        val map = MyFastJsonUtils.object2Map(memberAccount);
        redisService.hSetAll(key, map, expire);
    }

    @Override
    public void setByKey(String key, MemberAccount memberAccount, long expire) {

    }

    @Override
    public boolean existByKey(String key) {
        return redisService.hasKey(key);
    }

    @Override
    public List<MemberAccount> listByKey(String key, long pageNum, long pageSize) {
        return null;
    }

    @Override
    public void delByKey(String key) {
        redisService.del(key);
    }

    @Override
    public List<MemberAccount> listAllByKey(String key) {
        return null;
    }

    @Override
    public void lPushAllByCollectionByKey(String key, List<MemberAccount> list) {

    }

    @Override
    public MemberAccount get(Long id) {
        return null;
    }

    @Override
    public void set(Long id, MemberAccount memberAccount) {

    }

    @Override
    public boolean exist(Long id) {
        return false;
    }

    @Override
    public List<MemberAccount> list(long pageNum, long pageSize) {
        return null;
    }

    @Override
    public List<MemberAccount> listAll() {
        return null;
    }

    @Override
    public void del(Long id) {

    }

    @Override
    public void delByUserName(String username) {
        val key = RedisKeyConstants.MEMBER_ACCOUNT + username;
        redisService.del(key);
    }
}
